package bat.iog.hytool.fileHandler;

import java.io.*;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ZipHandle {


    public static void main(String[] args) {
        //要进行解压缩的zip文件
        File zipFile = new File("D:\\games\\1-201023201325.zip");

        //创建解压缩文件
        //获取zip文件的名称
        String zipFileName = zipFile.getName();

        //根据zip文件名称，提取压缩目录的名称
        String targetDirName = zipFileName.substring(0,zipFileName.indexOf("."));

        //创建解压缩文件目录
        File targetDir = new File(zipFile.getParent() + "\\" + targetDirName);

        if(!targetDir.exists()) {
            targetDir.mkdir();//创建文件
        }
        //2.解析读取Zip文件

        try (ZipInputStream in = new ZipInputStream(
                new FileInputStream("D:\\games\\1-201023201325.zip"), Charset.forName("gbk"))) {
            ZipEntry zipEntry = null;
            while((zipEntry = in.getNextEntry()) != null) {
                //获取zip压缩包中的子文件名称
                String zipEntryFileName = zipEntry.getName();
                
                
                zipEntry.isDirectory();
                
                //创建该文件的输出流
                String zipFilePath = targetDir.getPath() + "//" + zipEntryFileName;

                //定义输出流在try块里，结束并自动清空缓存区
                try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(zipFilePath))){
                    //读取该子文件的字节内容
                    byte[] buff = new byte[1024];
                    int len = -1;
                    while((len = in.read(buff)) != -1) {
                        bos.write(buff,0,len);
                    }
                }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
